/****************************************
Figure 4, Table A.5, Figure 5,
Table A.6, Figure A.1, Table A.7, 
Figure A.2, Table A.8, Figure A.3,
Figure A.8, Figure A.7, Table A.13,
Table A.18, Figure A.13
*****************************************/

* Load Census Data Cleaned and Collapsed
use "$dir/Data/Final/final_collapsed_clean.dta", clear
	
gen i = birthstatefip
gen t = (birthdecade/10) + 1 // someone who is 10 when ban is put in place is treated (time 0)
gen Ei = floor(year_of_ban/10) 	// decade when unit is first treated
gen K = t-Ei 			// "relative time" num decades since treated (could be missing if never-treated)


forvalues l = 1/6 {
	gen A`l'event = K==-`l'
}

forvalues l = 0/6 {
	gen P`l'event = K==`l'
}


gen insample6treat = abs(K) <= 6 & K != .


* Interaction treatment vars
forvalues pre = 6(-1)2 {
	cap n gen cmH_Pre`pre' = cmH * A`pre'event
}

forvalues post = 0/6 {
	cap n gen cmH_Post`post' = cmH * P`post'event
}

forvalues x = 0/6 {
	cap label var cmH_Pre`x' "Pre `x'"
	cap label var cmH_Post`x' "Post `x'"
}



replace Ei = . if cmH == 0 /* Treating low CM surnames as never having faced a ban */
gen nevertreated = Ei == . /* this includes low CM surnames and those that never faced a ban */

drop if year_of_ban > 1940 /* this drops all states that banned after 1940 or never banned because they
have year of ban value missing, so control group is low cousin marriage surnames in states that banned
by 1940 */


keep if insample6treat == 1


** Transforming and labelling outcome variables **


* Tranformations
gen ln_citypop = ln(citypop)
gen ln_occscore = ln(occscore)
gen ln_inchat = log(inchat_levels)
drop inchat_levels
gen occ_disp = 1 - occ_disp_hhi 
gen occ_disp_10digit = 1 - occ_disp_hhi_10digit 
gen occ_disp_occ_class = 1 - occ_disp_hhi_occ_class
gen geog_disp_state = 1 - geog_disp_hhi_state
gen geog_disp_county = 1 - geog_disp_hhi_county
gen geog_disp_enumdist = 1 - geog_disp_hhi_enumdist
gen share_ch_uncmn = 1 - share_ch_cmn 


* Scaling genetic outcomes
foreach var of varlist(gq_inst-genetic_d){
	qui: replace `var' = `var'*10000
}

* Labels 
label var birthdecade "Birth decade"
label var cmH ">10% cousin marriage rate (-1858)"
label var birthstatefip "Birth state"
label var resstatefip "Residence state"
label var year "Census year"
label var count_dor "Num census individuals in cell"


label var occscore "Occscore"
label var inchat "LIDO Income (Original)"
label var ln_inchat "LIDO Income"
label var citypop "City size"
label var urban "Urban"
label var farm "Residence: Farm"
label var mig_life "Interstate Migration"
label var mig_life_noban "Interstate Migration (State without ban)"
label var mig_life_ban "Interstate Migration (State with ban)"
label var ln_occscore "Income (log occscore)"
label var ln_citypop "Urbanization (log residence pop'n)"
label var divorced "Divorced"
label var mult_genh "Multigenerational HH"
label var nchild "No. of resident children"
label var nchlt5 "No. of resident children (under 5)"
label var agemarr "Age of Marriage"
label var durmarr "Duration of Current Marriage"
label var ncouples "No. of couples in unit"
label var ncouples_wo "No. of couples in unit (w/o own)"
label var nfams "No. of unrelated families in unit"
label var famsize "Family Size"
label var nsibs "No. of siblings in unit"
label var nm_single "Never Married/Single"
label var occ_disp_hhi "Occupational Dispersion HHI"
label var occ_disp_hhi_10digit "Occupational Dispersion HHI (10-digit codes)"
label var occ_disp_hhi_occ_class "Occupational Dispersion HHI (Farmer, White Collar, Blue Collar, Self-Employed)"
label var prank "Income percentile-rank"
label var geog_disp_hhi_state "Geographical Dispersion (State)"
label var geog_disp_hhi_county "Geographical Dispersion (County)"
label var geog_disp_hhi_enumdist "Geographical Dispersion (Enumeration District)"
label var occ_disp "Occupational Dispersion (HHI)"
label var occ_disp_10digit "Occupational Dispersion (HHI) (10-digit)"
label var occ_disp_occ_class "Occupational Dispersion (HHI) - White/Blue Collar, Farmer, Self-Employed"
label var geog_disp_state "Geographical Dispersion (HHI) - State"
label var geog_disp_county "Geographical Dispersion (HHI) - County"
label var geog_disp_enumdist "Geographical Dispersion (HHI) - Enumeration District"
label var gq_inst "Institutionalized, per 10,000"
label var gq_inst_med "Institutionalized (Medical), per 10,000"
label var gq_poorhouse "Institutionalized (Poorhouse), per 10,000"
label var hmem_disable "Household member (Disabled), per 10,000"
label var blind_deaf "Blind/Deaf, per 10,000"
label var genetic_d "Institutionalized (Medical) + Disability, per 10,000"
label var share_ch_cmn "Share childr. with common names"
label var share_ch_uncmn "Share childr. with uncommon names"

foreach v of varlist(ln_occscore_nm-genetic_d_nm){
	label var `v' "Number of individuals with non-missing outcome in cell"
}


compress


/**************************************
Figure 4
**************************************/

eststo clear
local outcomes ln_citypop farm urban mig_life


foreach outcome in `outcomes' {
	eventstudyinteract `outcome'  ///
	cmH ///
    cmH_P* ///
	[aweight = `outcome'_nm] , absorb(i.i##i.t i.year i.i##i.cmH) vce(cluster i) ///
	cohort(Ei) control_cohort(nevertreated)
	su `outcome' [aw = `outcome'_nm]if e(sample) == 1
	local omean = string(round(r(mean), .01))
	
 	matrix b_iw = e(b_iw) // Save the results for table output
 	matrix V_iw = e(V_iw)
	capture matrix V_iw = diag(e(V_iw))
 	erepost b = b_iw V = V_iw // ssc install erepost
 	esttab
	local bound = round(abs(_b[cmH_Post6]) + _se[cmH_Post6]*1.96, .01) 
	* this just a manual way of making sure that ylabels are symmetric around 0 *
	local rnge = `bound'/3
	local rnge_use = round(`rnge', .01)
	
	event_plot e(b_iw)#e(V_iw), default_look ///
	stub_lag(cmH_Post#) stub_lead(cmH_Pre#) ///
		graph_opt(ytitle("`: variable label `outcome'' (mean = `omean')") xtitle("Birth decade relative to ban (+1)") xlabel(-6(1)6) ylabel(-`bound'(`rnge_use')`bound') legend(off) )
	graph copy ngSA`outcome', replace
		}

		
		global sum

foreach outcome of local outcomes{
    global sum $sum ngSA`letter'`outcome'
}
graph combine $sum ,  title("")  xcommon // subtitle("(Sun & Abraham 2021)") rows(1) ysize(1.3) xsize(3) 





graph export "Output/TablesFigures/Fig_Residence.png", replace


/**************************************
Table A.5
**************************************/

eststo clear
eventstudyinteract ln_citypop ///
	cmH ///
    cmH_P* ///
	[aweight = ln_citypop_nm] , absorb(i.i##i.t i.year i.i##i.cmH) vce(cluster i) ///
	cohort(Ei) control_cohort(nevertreated)
	
 	matrix b_iw = e(b_iw) // Save the results for table output
 	matrix V_iw = e(V_iw)
	capture matrix V_iw = diag(e(V_iw))
 	erepost b = b_iw V = V_iw // ssc install erepost
 	esttab
	eststo A1
	sum ln_citypop [fw = ln_citypop_nm]
	local obs1 =  r(N)
    su ln_citypop [aw = ln_citypop_nm] if e(sample) == 1
    local meanA = string(round(r(mean), .01))
 
 
eventstudyinteract farm ///
	cmH ///
    cmH_P* ///
	[aweight = farm_nm] , absorb(i.i##i.t i.year i.i##i.cmH) vce(cluster i) ///
	cohort(Ei) control_cohort(nevertreated)
	
 	matrix b_iw = e(b_iw) // Save the results for table output
 	matrix V_iw = e(V_iw)
	capture matrix V_iw = diag(e(V_iw))
 	erepost b = b_iw V = V_iw // ssc install erepost
 	esttab
	eststo A2
	sum farm [fw = farm_nm]
	local obs2 = r(N)
    su farm [aw = farm_nm] if e(sample) == 1
    local meanB = string(round(r(mean), .01))
	
	
eventstudyinteract urban ///
	cmH ///
    cmH_P* ///
	[aweight = urban_nm] , absorb(i.i##i.t i.year i.i##i.cmH) vce(cluster i) ///
	cohort(Ei) control_cohort(nevertreated)
	
 	matrix b_iw = e(b_iw) // Save the results for table output
 	matrix V_iw = e(V_iw)
	capture matrix V_iw = diag(e(V_iw))
 	erepost b = b_iw V = V_iw // ssc install erepost
 	esttab
	eststo A3
	sum urban [fw = urban_nm]
	local obs3 =  r(N)
    su urban [aw = urban_nm] if e(sample) == 1
    local meanC = string(round(r(mean), .01))
 
 
eventstudyinteract mig_life ///
	cmH ///
    cmH_P* ///
	[aweight = mig_life_nm] , absorb(i.i##i.t i.year i.i##i.cmH) vce(cluster i) ///
	cohort(Ei) control_cohort(nevertreated)
	
 	matrix b_iw = e(b_iw) // Save the results for table output
 	matrix V_iw = e(V_iw)
	capture matrix V_iw = diag(e(V_iw))
 	erepost b = b_iw V = V_iw // ssc install erepost
 	esttab
	eststo A4
	sum mig_life [fw = mig_life_nm]
	local obs4 = r(N)
    su mig_life [aw = mig_life_nm] if e(sample) == 1
    local meanD = string(round(r(mean), .01))
 


estout A1 A2 A3 A4 using "$dir/Output/TablesFigures/results_residence.tex", style(tex) replace ///
    keep(cmH_*) ///
	cells(b(star fmt(%9.4f)) se(par)) ///
	nolabel collabels(none) mlabels(none) starlevels(* 0.10 ** 0.05 *** 0.01) ///
	varlabels(cmH_Pre6 "-6" cmH_Pre5 "-5" ///
	cmH_Pre4 "-4" cmH_Pre3 "-3" ///
	cmH_Pre2 "-2" cmH_Post0 "0" ///
	cmH_Post1 "1" cmH_Post2 "2" ///
	cmH_Post3 "3" cmH_Post4 "4" ///
	cmH_Post5 "5" cmH_Post6 "6")

local tex " \\ \hline"
local tex "`tex' Observations & `obs1' & `obs2'  & `obs3' & `obs4' \\"
local tex "`tex' Mean Dep. Var & `meanA' & `meanB'  & `meanC' & `meanD'   \\"
local tex "`tex' State X Birth Decade FE & Yes & Yes  & Yes & Yes \\"
local tex "`tex' State X High CM F.E. & Yes & Yes  & Yes & Yes  \\"
local tex "`tex' Census year F.E. & Yes & Yes  & Yes & Yes \\"
local tex "`tex' \multicolumn{5}{p{8cm}}{\tiny \textit{Notes:} Standard errors clustered at state-level."
local tex "`tex' *** p<0.01, ** p<0.05, * p<0.1.} \\ \end{tabular} }"
	
esttab A1 A2  A3 A4  using "$dir/Output/TablesFigures/results_residencea.tex", style(tex) replace booktabs ///
	d(*) nolabel collabels(none) noobs postfoot("`tex'") nonum ///
	mtitles("(1)" "(2)" "(3)" "(4)") ///
	mgroups("Urbanization (log residence pop'n')" "Farm" "Urban" "Interstate Migration", pattern(1 1 1 1) ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
	
/**************************************
Figure 5
**************************************/

eststo clear
local outcomes ln_occscore ln_inchat prank  


foreach outcome in `outcomes' {
	eventstudyinteract `outcome'  ///
	cmH ///
    cmH_P* ///
	[aweight = ln_occscore_nm] , absorb(i.i##i.t i.year i.i##i.cmH) vce(cluster i) ///
	cohort(Ei) control_cohort(nevertreated)
	su `outcome' [aw = ln_occscore_nm] if e(sample) == 1
	local omean = string(round(r(mean), .01))
	
 	matrix b_iw = e(b_iw) // Save the results for table output
 	matrix V_iw = e(V_iw)
	capture matrix V_iw = diag(e(V_iw))
 	erepost b = b_iw V = V_iw // ssc install erepost
 	esttab
	local bound = round(abs(_b[cmH_Post6]) + _se[cmH_Post6]*1.96, .01)
	local rnge = `bound'/3
	local rnge_use = round(`rnge', .01)
	
	event_plot e(b_iw)#e(V_iw), default_look ///
	stub_lag(cmH_Post#) stub_lead(cmH_Pre#) ///
		graph_opt(ytitle("`: variable label `outcome'' (mean = `omean')") xtitle("Birth decade relative to ban (+1)") xlabel(-6(1)6) ylabel(-`bound'(`rnge_use')`bound') legend(off) )
	graph copy ngSA`outcome', replace
		}

		
		global sum

foreach outcome of local outcomes{
    global sum $sum ngSA`letter'`outcome'
}
graph combine $sum ,  title("")  xcommon // subtitle("(Sun & Abraham 2021)") rows(1) ysize(1.3) xsize(3) 

graph export "Output/TablesFigures/Fig_Income.png", replace

/**************************************
Table A.6
**************************************/

eststo clear
eventstudyinteract ln_occscore  ///
	cmH ///
    cmH_P* ///
	[aweight = ln_occscore_nm] , absorb(i.i##i.t i.year i.i##i.cmH) vce(cluster i) ///
	cohort(Ei) control_cohort(nevertreated)
	
 	matrix b_iw = e(b_iw) // Save the results for table output
 	matrix V_iw = e(V_iw)
	capture matrix V_iw = diag(e(V_iw))
 	erepost b = b_iw V = V_iw // ssc install erepost
 	esttab
	eststo A1
    sum ln_occscore [fw = ln_occscore_nm]
	local obs1 = r(N)
    su ln_occscore [aw = ln_occscore_nm] if e(sample) == 1
    local meanA = string(round(r(mean), .01))
	
	

eventstudyinteract ln_inchat ///
	cmH ///
    cmH_P* ///
	[aweight = ln_occscore_nm] , absorb(i.i##i.t i.year i.i##i.cmH) vce(cluster i) ///
	cohort(Ei) control_cohort(nevertreated)
	
 	matrix b_iw = e(b_iw) // Save the results for table output
 	matrix V_iw = e(V_iw)
	capture matrix V_iw = diag(e(V_iw))
 	erepost b = b_iw V = V_iw // ssc install erepost
 	esttab
	eststo A2
    sum ln_inchat [fw = ln_occscore_nm]
	local obs2 = r(N)
    su ln_inchat [aw = ln_occscore_nm] if e(sample) == 1
    local meanB = string(round(r(mean), .01))
 
 
eventstudyinteract prank ///
	cmH ///
    cmH_P* ///
	[aweight = ln_occscore_nm] , absorb(i.i##i.t i.year i.i##i.cmH) vce(cluster i) ///
	cohort(Ei) control_cohort(nevertreated)
	
 	matrix b_iw = e(b_iw) // Save the results for table output
 	matrix V_iw = e(V_iw)
	capture matrix V_iw = diag(e(V_iw))
 	erepost b = b_iw V = V_iw // ssc install erepost
 	esttab
	eststo A3
	sum prank [fw = ln_occscore_nm]
	local obs3 = r(N)
    su prank [aw = ln_occscore_nm] if e(sample) == 1
    local meanC = string(round(r(mean), .01))
 

 estout A1 A2 A3 using "$dir/Output/TablesFigures/results_income.tex", style(tex) replace ///
    keep(cmH_*) ///
	cells(b(star fmt(%9.4f)) se(par)) ///
	nolabel collabels(none) mlabels(none) starlevels(* 0.10 ** 0.05 *** 0.01) ///
	varlabels(cmH_Pre6 "-6" cmH_Pre5 "-5" ///
	cmH_Pre4 "-4" cmH_Pre3 "-3" ///
	cmH_Pre2 "-2" cmH_Post0 "0" ///
	cmH_Post1 "1" cmH_Post2 "2" ///
	cmH_Post3 "3" cmH_Post4 "4" ///
	cmH_Post5 "5" cmH_Post6 "6")

local tex " \\ \hline"
local tex "`tex' Observations & `obs1' & `obs2' & & `obs3' \\"
local tex "`tex' Mean Dep. Var & `meanA' & `meanB' & `meanC'  \\"
local tex "`tex' State X Birth Decade FE & Yes & Yes & Yes \\"
local tex "`tex' State X High CM F.E. & Yes & Yes & Yes \\"
local tex "`tex' Census year F.E. & Yes & Yes & Yes \\"
local tex "`tex' \multicolumn{3}{p{8cm}}{\tiny \textit{}."
local tex "`tex' *** p<0.01, ** p<0.05, * p<0.1.} \\ \end{tabular} }"
	
esttab A1 A2 A3 using "$dir/Output/TablesFigures/results_incomea.tex", style(tex) replace booktabs ///
	d(*) nolabel collabels(none) noobs postfoot("`tex'") nonum ///
	mtitles("(1)" "(2)" "(3)") ///
	mgroups("Income Percentile-Rank" "Income (Log LIDO Score)" "Income (Log Occscore)", pattern(1 1 1) ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
	
/**************************************
Figure A.1
**************************************/

eststo clear
local outcomes geog_disp_enumdist occ_disp
foreach outcome in `outcomes' {
	eventstudyinteract `outcome'  ///
	cmH ///
    cmH_P* ///
	[aweight = count_dor] , absorb(i.i##i.t i.year i.i##i.cmH) vce(cluster i) ///
	cohort(Ei) control_cohort(nevertreated)
	su `outcome' [aw = count_dor]if e(sample) == 1
	local omean = string(round(r(mean), .01))
	
 	matrix b_iw = e(b_iw) // Save the results for table output
 	matrix V_iw = e(V_iw)
	capture matrix V_iw = diag(e(V_iw))
 	erepost b = b_iw V = V_iw // ssc install erepost
 	esttab
	
	event_plot e(b_iw)#e(V_iw), default_look ///
	stub_lag(cmH_Post#) stub_lead(cmH_Pre#) ///
		graph_opt(ytitle("`: variable label `outcome'' (mean = `omean')") xtitle("Birth decade relative to ban (+1)") xlabel(-6(1)6) ylabel(-0.1(0.05)0.1) legend(off) )
	graph copy ngSA`outcome', replace
		}

		
		global sum

foreach outcome of local outcomes{
    global sum $sum ngSA`letter'`outcome'
}
graph combine $sum ,  title("")  xcommon ycommon // subtitle("(Sun & Abraham 2021)") rows(1) ysize(1.3) xsize(3) 

graph export "Output/TablesFigures/Fig_ODisp.png", replace
	

/**************************************
Table A.7
**************************************/
	
eststo clear
eventstudyinteract geog_disp_enumdist  ///
	cmH ///
    cmH_P* ///
	[aweight = count_dor] , absorb(i.i##i.t i.year i.i##i.cmH) vce(cluster i) ///
	cohort(Ei) control_cohort(nevertreated)
	
	
    matrix b_iw = e(b_iw) // Save the results for table output
 	matrix V_iw = e(V_iw)
	capture matrix V_iw = diag(e(V_iw))
 	erepost b = b_iw V = V_iw // ssc install erepost
 	esttab
	eststo A1
	sum geog_disp_enumdist [fw = count_dor]
	local obs1 = r(N)
    sum geog_disp_enumdist [aw = count_dor] if e(sample) == 1
    local meanA = string(round(r(mean), .01))
	
	
	
eventstudyinteract occ_disp  ///
	cmH ///
    cmH_P* ///
	[aweight = count_dor] , absorb(i.i##i.t i.year i.i##i.cmH) vce(cluster i) ///
	cohort(Ei) control_cohort(nevertreated)
	
	
    matrix b_iw = e(b_iw) // Save the results for table output
 	matrix V_iw = e(V_iw)
	capture matrix V_iw = diag(e(V_iw))
 	erepost b = b_iw V = V_iw // ssc install erepost
 	esttab
	eststo A2
	sum occ_disp [fw = count_dor]
	local obs2 = r(N)
    sum occ_disp [aw = count_dor] if e(sample) == 1
    local meanB = string(round(r(mean), .01))
 
 

 estout A1 A2 using "$dir/Output/TablesFigures/results_godisp.tex", style(tex) replace ///
    keep(cmH_*) ///
	cells(b(star fmt(%9.4f)) se(par)) ///
	nolabel collabels(none) mlabels(none) starlevels(* 0.10 ** 0.05 *** 0.01) ///
	varlabels(cmH_Pre6 "-6" cmH_Pre5 "-5" ///
	cmH_Pre4 "-4" cmH_Pre3 "-3" ///
	cmH_Pre2 "-2" cmH_Post0 "0" ///
	cmH_Post1 "1" cmH_Post2 "2" ///
	cmH_Post3 "3" cmH_Post4 "4" ///
	cmH_Post5 "5" cmH_Post6 "6")

local tex " \\ \hline"
local tex "`tex' Observations & `obs1' & `obs2' \\"
local tex "`tex' Mean Dep. Var & `meanA' & `meanB' \\"
local tex "`tex' State X Birth Decade FE & Yes & Yes \\"
local tex "`tex' State X High CM F.E. & Yes & Yes \\"
local tex "`tex' Census year F.E. & Yes & Yes \\"
local tex "`tex' \multicolumn{5}{p{8cm}}{\tiny \textit{Notes:} Standard errors clustered at state-level."
local tex "`tex' *** p<0.01, ** p<0.05, * p<0.1.} \\ \end{tabular} }"
	
esttab A1  using "$dir/Output/TablesFigures/results_gdispa.tex", style(tex) replace booktabs ///
	d(*) nolabel collabels(none) noobs postfoot("`tex'") nonum ///
	mtitles("(1)" ("2")) ///
	mgroups("Geographical Dispersion (EnumDist)" "Occupational Dispersion (1950 occ. codes)", pattern(1 1) ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))

/**************************************
Figure A.2
**************************************/

eststo clear
local outcomes famsize ncouples_wo nchlt5 mult_genh 


foreach outcome in `outcomes' {
	eventstudyinteract `outcome'  ///
	cmH ///
    cmH_P* ///
	[aweight = `outcome'_nm] ///
	, absorb(i.i##i.t i.year i.i##i.cmH) vce(cluster i) ///
	cohort(Ei) control_cohort(nevertreated)
	su `outcome' [aw = `outcome'_nm]if e(sample) == 1
	local omean = string(round(r(mean), .01))
	
 	matrix b_iw = e(b_iw) // Save the results for table output
 	matrix V_iw = e(V_iw)
	capture matrix V_iw = diag(e(V_iw))
 	erepost b = b_iw V = V_iw // ssc install erepost
 	esttab
	local bound = max(round(abs(_b[cmH_Post6]) + _se[cmH_Post6]*1.96, .01), 0.01)
	local rnge = `bound'/3
	local rnge_use = round(`rnge', .001)
	
	event_plot e(b_iw)#e(V_iw), default_look ///
	stub_lag(cmH_Post#) stub_lead(cmH_Pre#) ///
		graph_opt(ytitle("`: variable label `outcome'' (mean = `omean')") xtitle("Birth decade relative to ban (+1)") xlabel(-6(1)6) ylabel(-`bound'(`rnge_use')`bound') legend(off) )
	graph copy ngSA`outcome', replace
		}

		
		global sum

foreach outcome of local outcomes{
    global sum $sum ngSA`letter'`outcome'
}
graph combine $sum ,  title("") xcommon // subtitle("(Sun & Abraham 2021)") rows(1) ysize(1.3) xsize(3) 

graph export "$dir/Output/TablesFigures/Fig_Fam.png", replace


/**************************************
Table A.8
**************************************/

eststo clear
eventstudyinteract famsize ///
	cmH ///
    cmH_P* ///
	[aweight = famsize_nm] , absorb(i.i##i.t i.year i.i##i.cmH) vce(cluster i) ///
	cohort(Ei) control_cohort(nevertreated)
	
 	matrix b_iw = e(b_iw) // Save the results for table output
 	matrix V_iw = e(V_iw)
	capture matrix V_iw = diag(e(V_iw))
 	erepost b = b_iw V = V_iw // ssc install erepost
 	esttab
	eststo A1
	sum famsize [fw = famsize_nm]
	local obs1 =  r(N)
    su famsize [aw = famsize_nm] if e(sample) == 1
    local meanA = string(round(r(mean), .01))
 
 
eventstudyinteract ncouples_wo ///
	cmH ///
    cmH_P* ///
	[aweight = ncouples_wo_nm] , absorb(i.i##i.t i.year i.i##i.cmH) vce(cluster i) ///
	cohort(Ei) control_cohort(nevertreated)
	
 	matrix b_iw = e(b_iw) // Save the results for table output
 	matrix V_iw = e(V_iw)
	capture matrix V_iw = diag(e(V_iw))
 	erepost b = b_iw V = V_iw // ssc install erepost
 	esttab
	eststo A2
	sum ncouples_wo [fw = ncouples_wo_nm]
	local obs2 = r(N)
    su ncouples_wo[aw = ncouples_wo_nm] if e(sample) == 1
    local meanB = string(round(r(mean), .01))
	
	
eventstudyinteract nchlt5  ///
	cmH ///
    cmH_P* ///
	[aweight = nchlt5_nm] , absorb(i.i##i.t i.year i.i##i.cmH) vce(cluster i) ///
	cohort(Ei) control_cohort(nevertreated)
	
 	matrix b_iw = e(b_iw) // Save the results for table output
 	matrix V_iw = e(V_iw)
	capture matrix V_iw = diag(e(V_iw))
 	erepost b = b_iw V = V_iw // ssc install erepost
 	esttab
	eststo A3
	sum nchlt5 [fw = nchlt5_nm]
	local obs3 =  r(N)
    su nchlt5  [aw = nchlt5_nm] if e(sample) == 1
    local meanC = string(round(r(mean), .01))
 
 
eventstudyinteract 	mult_genh ///
	cmH ///
    cmH_P* ///
	[aweight = mult_genh_nm] , absorb(i.i##i.t i.year i.i##i.cmH) vce(cluster i) ///
	cohort(Ei) control_cohort(nevertreated)
	
 	matrix b_iw = e(b_iw) // Save the results for table output
 	matrix V_iw = e(V_iw)
	capture matrix V_iw = diag(e(V_iw))
 	erepost b = b_iw V = V_iw // ssc install erepost
 	esttab
	eststo A4
	sum mult_genh [fw = mult_genh_nm]
	local obs4 = r(N)
    su 	mult_genh [aw = mult_genh_nm] if e(sample) == 1
    local meanD = string(round(r(mean), .01))
 

 estout A1 A2 A3 A4 using "$dir/Output/TablesFigures/results_family.tex", style(tex) replace ///
    keep(cmH_*) ///
	cells(b(star fmt(%9.4f)) se(par)) ///
	nolabel collabels(none) mlabels(none) starlevels(* 0.10 ** 0.05 *** 0.01) ///
	varlabels(cmH_Pre6 "-6" cmH_Pre5 "-5" ///
	cmH_Pre4 "-4" cmH_Pre3 "-3" ///
	cmH_Pre2 "-2" cmH_Post0 "0" ///
	cmH_Post1 "1" cmH_Post2 "2" ///
	cmH_Post3 "3" cmH_Post4 "4" ///
	cmH_Post5 "5" cmH_Post6 "6")

local tex " \\ \hline"
local tex "`tex' Observations & `obs1' & `obs2'  & `obs3' & `obs4' \\"
local tex "`tex' Mean Dep. Var & `meanA' & `meanB'  & `meanC' & `meanD'   \\"
local tex "`tex' State X Birth Decade FE & Yes & Yes  & Yes & Yes \\"
local tex "`tex' State X High CM F.E. & Yes & Yes  & Yes & Yes  \\"
local tex "`tex' Census year F.E. & Yes & Yes  & Yes & Yes \\"
local tex "`tex' \multicolumn{5}{p{8cm}}{\tiny \textit{Notes:} Standard errors clustered at state-level."
local tex "`tex' *** p<0.01, ** p<0.05, * p<0.1.} \\ \end{tabular} }"
	
esttab A1 A2  A3 A4  using "$dir/Output/TablesFigures/results_familya.tex", style(tex) replace booktabs ///
	d(*) nolabel collabels(none) noobs postfoot("`tex'") nonum ///
	mtitles("(1)" "(2)" "(3)" "(4)") ///
	mgroups("Family Size" "No. of children (under 5)" "No. of couples" "Multigenerational HH", pattern(1 1 1 1) ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))

	
/**************************************
Figure A.3
**************************************/

local outcomes gq_inst

foreach outcome in `outcomes' {
	eventstudyinteract `outcome'  ///
	cmH ///
    cmH_P* ///
	[aweight = `outcome'_nm] , absorb(i.i##i.t i.year i.i##i.cmH) vce(cluster i) ///
	cohort(Ei) control_cohort(nevertreated)
	su `outcome' [aw =  `outcome'_nm]  if e(sample) == 1
    local meanA = string(round(r(mean), .1))

	
 	matrix b_iw = e(b_iw) // Save the results for table output
 	matrix V_iw = e(V_iw)
	capture matrix V_iw = diag(e(V_iw))
 	erepost b = b_iw V = V_iw // ssc install erepost
 	esttab
	local bound = round(abs(_b[cmH_Post6]) + _se[cmH_Post6]*1.96, .1)
	local rnge = `bound'/3
	local rnge_use = round(`rnge', .01)
	
	
	event_plot e(b_iw)#e(V_iw), default_look ///
	stub_lag(cmH_Post#) stub_lead(cmH_Pre#) ///
		graph_opt(ytitle("`: variable label `outcome'' (mean = `meanA')") xtitle("Birth decade relative to ban (+1)") xlabel(-6(1)6) ylabel(-`bound'(`rnge_use')`bound') legend(off) )
	graph copy ngSA`outcome', replace
		}

		
		global sum

foreach outcome of local outcomes{
    global sum $sum ngSA`letter'`outcome'
}
graph combine $sum ,  title("")  xcommon // subtitle("(Sun & Abraham 2021)") rows(1) ysize(1.3) xsize(3) 

graph export "Output/TablesFigures/Fig_Resultsinst.png", replace

* Table with institutionalization outcome (Table A.9)
eststo clear
eventstudyinteract gq_inst ///
	cmH ///
    cmH_P* ///
	[aweight = gq_inst_nm] , absorb(i.i##i.t i.year i.i##i.cmH) vce(cluster i) ///
	cohort(Ei) control_cohort(nevertreated)
	
 	matrix b_iw = e(b_iw) // Save the results for table output
 	matrix V_iw = e(V_iw)
	capture matrix V_iw = diag(e(V_iw))
 	erepost b = b_iw V = V_iw // ssc install erepost
 	esttab
	eststo A1
	sum gq_inst [fw = gq_inst_nm]
	local obs1 =  r(N)
    su gq_inst [aw =  gq_inst_nm] if e(sample) == 1
    local meanA = string(round(r(mean), .01))
	
	
 estout A1 using "$dir/Output/TablesFigures/results_inst.tex", style(tex) replace ///
    keep(cmH_*) ///
	cells(b(star fmt(%9.4f)) se(par)) ///
	nolabel collabels(none) mlabels(none) starlevels(* 0.10 ** 0.05 *** 0.01) ///
	varlabels(cmH_Pre6 "-6" cmH_Pre5 "-5" ///
	cmH_Pre4 "-4" cmH_Pre3 "-3" ///
	cmH_Pre2 "-2" cmH_Post0 "0" ///
	cmH_Post1 "1" cmH_Post2 "2" ///
	cmH_Post3 "3" cmH_Post4 "4" ///
	cmH_Post5 "5" cmH_Post6 "6")

local tex " \\ \hline"
local tex "`tex' Observations & `obs1' \\"
local tex "`tex' Mean Dep. Var & `meanA'   \\"
local tex "`tex' State X Birth Decade FE & Yes  \\"
local tex "`tex' State X High CM F.E. & Yes  \\"
local tex "`tex' Census year F.E. & Yes  \\"
local tex "`tex' \multicolumn{5}{p{8cm}}{\tiny \textit{Notes:} Standard errors clustered at state-level."
local tex "`tex' *** p<0.01, ** p<0.05, * p<0.1.} \\ \end{tabular} }"
	
esttab A1 using "$dir/Output/TablesFigures/results_insta.tex", style(tex) replace booktabs ///
	d(*) nolabel collabels(none) noobs postfoot("`tex'") nonum ///
	mtitles("(1)") ///
	mgroups("Institutionalized", pattern(1) ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))

	
/**************************************
Figure A.7
**************************************/

eststo clear

local outcomes share_ch_uncmn


foreach outcome in `outcomes' {
	eventstudyinteract `outcome'  ///
	cmH ///
    cmH_P* ///
	[aweight = count_dor] , absorb(i.i##i.t i.year i.i##i.cmH) vce(cluster i) ///
	cohort(Ei) control_cohort(nevertreated)
	su `outcome' [aw = count_dor]if e(sample) == 1
	local omean = string(round(r(mean), .01))
	
 	matrix b_iw = e(b_iw) // Save the results for table output
 	matrix V_iw = e(V_iw)
	capture matrix V_iw = diag(e(V_iw))
 	erepost b = b_iw V = V_iw // ssc install erepost
 	esttab
	local bound = round(abs(_b[cmH_Post6]) + _se[cmH_Post6]*1.96, .001)
	local rnge = `bound'/3
	local rnge_use = round(`rnge', .01)
	
	event_plot e(b_iw)#e(V_iw), default_look ///
	stub_lag(cmH_Post#) stub_lead(cmH_Pre#) ///
		graph_opt(ytitle("`: variable label `outcome'' (mean = `omean')") xtitle("Birth decade relative to ban (+1)") xlabel(-6(1)6) ylabel(-0.03(0.015)0.03) legend(off) )
	graph copy ngSA`outcome', replace
		}

		
		global sum

foreach outcome of local outcomes{
    global sum $sum ngSA`letter'`outcome'
}
graph combine $sum ,  title("")  xcommon // subtitle("(Sun & Abraham 2021)") rows(1) ysize(1.3) xsize(3) 

graph export "$dir/Output/TablesFigures/Fig_Indiv.png", replace


/**************************************
Table A.13
**************************************/

eststo clear
eventstudyinteract share_ch_uncmn ///
	cmH ///
    cmH_P* ///
	[aweight = count_dor] , absorb(i.i##i.t i.year i.i##i.cmH) vce(cluster i) ///
	cohort(Ei) control_cohort(nevertreated)
	
 	matrix b_iw = e(b_iw) // Save the results for table output
 	matrix V_iw = e(V_iw)
	capture matrix V_iw = diag(e(V_iw))
 	erepost b = b_iw V = V_iw // ssc install erepost
 	esttab
	eststo A1
	sum share_ch_uncmn [fw = count_dor] if e(sample) == 1
	local obs1 =  r(N)
    su share_ch_uncmn  [aw = count_dor] if e(sample) == 1
    local meanA = string(round(r(mean), .01))
 
 
    estout A1 using "$dir/Output/TablesFigures/results_indiv.tex", style(tex) replace ///
    keep(cmH_*) ///
	cells(b(star fmt(%9.4f)) se(par)) ///
	nolabel collabels(none) mlabels(none) starlevels(* 0.10 ** 0.05 *** 0.01) ///
	varlabels(cmH_Pre6 "-6" cmH_Pre5 "-5" ///
	cmH_Pre4 "-4" cmH_Pre3 "-3" ///
	cmH_Pre2 "-2" cmH_Post0 "0" ///
	cmH_Post1 "1" cmH_Post2 "2" ///
	cmH_Post3 "3" cmH_Post4 "4" ///
	cmH_Post5 "5" cmH_Post6 "6")

local tex " \\ \hline"
local tex "`tex' Observations & `obs1' \\"
local tex "`tex' Mean Dep. Var & `meanA'   \\"
local tex "`tex' State X Birth Decade FE & Yes  \\"
local tex "`tex' State X High CM F.E. & Yes  \\"
local tex "`tex' Census year F.E. & Yes \\"
local tex "`tex' \multicolumn{5}{p{8cm}}{\tiny \textit{Notes:} Standard errors clustered at state-level."
local tex "`tex' *** p<0.01, ** p<0.05, * p<0.1.} \\ \end{tabular} }"
	
esttab A1 using "$dir/Output/TablesFigures/results_indiva.tex", style(tex) replace booktabs ///
	d(*) nolabel collabels(none) noobs postfoot("`tex'") nonum ///
	mtitles("(1)") ///
	mgroups("Share of children with uncommon names", pattern(1) ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))

	
/**************************************
Figure A.8
**************************************/

eststo clear

local outcomes genetic_d

foreach outcome in `outcomes' {
	eventstudyinteract `outcome'  ///
	cmH ///
    cmH_P* ///
	[aweight = `outcome'_nm] , absorb(i.i##i.t i.year i.i##i.cmH) vce(cluster i) ///
	cohort(Ei) control_cohort(nevertreated)
	su `outcome' [aw =  `outcome'_nm]  if e(sample) == 1
    local meanA = string(round(r(mean), .1))

	
 	matrix b_iw = e(b_iw) // Save the results for table output
 	matrix V_iw = e(V_iw)
	capture matrix V_iw = diag(e(V_iw))
 	erepost b = b_iw V = V_iw // ssc install erepost
 	esttab
	local bound = round(abs(_b[cmH_Post6]) + _se[cmH_Post6]*1.96, .1)
	local rnge = `bound'/3
	local rnge_use = round(`rnge', .01)
	
	
	event_plot e(b_iw)#e(V_iw), default_look ///
	stub_lag(cmH_Post#) stub_lead(cmH_Pre#) ///
		graph_opt(ytitle("`: variable label `outcome'' (mean = `meanA')") xtitle("Birth decade relative to ban (+1)") xlabel(-6(1)6) ylabel(-`bound'(`rnge_use')`bound') legend(off) )
	graph copy ngSA`outcome', replace
		}

		
		global sum

foreach outcome of local outcomes{
    global sum $sum ngSA`letter'`outcome'
}
graph combine $sum ,  title("")  xcommon // subtitle("(Sun & Abraham 2021)") rows(1) ysize(1.3) xsize(3) 

graph export "Output/TablesFigures/Fig_ResultsGenD.png", replace


/**************************************
Table A.14
**************************************/

eststo clear
eventstudyinteract genetic_d ///
	cmH ///
    cmH_P* ///
	[aweight = genetic_d_nm] , absorb(i.i##i.t i.year i.i##i.cmH) vce(cluster i) ///
	cohort(Ei) control_cohort(nevertreated)
	
 	matrix b_iw = e(b_iw) // Save the results for table output
 	matrix V_iw = e(V_iw)
	capture matrix V_iw = diag(e(V_iw))
 	erepost b = b_iw V = V_iw // ssc install erepost
 	esttab
	eststo A1
	sum genetic_d [fw = genetic_d_nm]
	local obs1 =  r(N)
    su genetic_d [aw = genetic_d_nm] if e(sample) == 1
    local meanA = string(round(r(mean), .01))
	
	
 estout A1 using "$dir/Output/TablesFigures/results_gend.tex", style(tex) replace ///
    keep(cmH_*) ///
	cells(b(star fmt(%9.4f)) se(par)) ///
	nolabel collabels(none) mlabels(none) starlevels(* 0.10 ** 0.05 *** 0.01) ///
	varlabels(cmH_Pre6 "-6" cmH_Pre5 "-5" ///
	cmH_Pre4 "-4" cmH_Pre3 "-3" ///
	cmH_Pre2 "-2" cmH_Post0 "0" ///
	cmH_Post1 "1" cmH_Post2 "2" ///
	cmH_Post3 "3" cmH_Post4 "4" ///
	cmH_Post5 "5" cmH_Post6 "6")

local tex " \\ \hline"
local tex "`tex' Observations & `obs1' \\"
local tex "`tex' Mean Dep. Var & `meanA'   \\"
local tex "`tex' State X Birth Decade FE & Yes  \\"
local tex "`tex' State X High CM F.E. & Yes  \\"
local tex "`tex' Census year F.E. & Yes  \\"
local tex "`tex' \multicolumn{5}{p{8cm}}{\tiny \textit{Notes:} Standard errors clustered at state-level."
local tex "`tex' *** p<0.01, ** p<0.05, * p<0.1.} \\ \end{tabular} }"
	
esttab A1 using "$dir/Output/TablesFigures/results_genda.tex", style(tex) replace booktabs ///
	d(*) nolabel collabels(none) noobs postfoot("`tex'") nonum ///
	mtitles("(1)") ///
	mgroups("Institutionalized", pattern(1) ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
	

/**************************************
Table A.18
**************************************/

eststo clear
eventstudyinteract ln_citypop ///
	cmH ///
    cmH_P* ///
	[aweight = ln_citypop_nm] , absorb(i.i##i.t i.year i.cmH#c.t  i.i##i.cmH) vce(cluster i) ///
	cohort(Ei) control_cohort(nevertreated)
	
 	matrix b_iw = e(b_iw) // Save the results for table output
 	matrix V_iw = e(V_iw)
	capture matrix V_iw = diag(e(V_iw))
 	erepost b = b_iw V = V_iw // ssc install erepost
 	esttab
	eststo A1
    sum ln_citypop [fw = ln_citypop_nm]
	local obs1 = r(N)
    su ln_citypop [aw = ln_citypop_nm] if e(sample) == 1
    local meanA = string(round(r(mean), .01))
 
 
eventstudyinteract ln_occscore ///
	cmH ///
    cmH_P* ///
	[aweight = ln_occscore_nm] , absorb(i.i##i.t i.year i.cmH#c.t i.i##i.cmH) vce(cluster i) ///
	cohort(Ei) control_cohort(nevertreated)
	
 	matrix b_iw = e(b_iw) // Save the results for table output
 	matrix V_iw = e(V_iw)
	capture matrix V_iw = diag(e(V_iw))
 	erepost b = b_iw V = V_iw // ssc install erepost
 	esttab
	eststo A2
	sum ln_occscore [fw = ln_occscore_nm]
	local obs2 = r(N)
    su ln_occscore [aw = ln_occscore_nm] if e(sample) == 1
    local meanB = string(round(r(mean), .01))
 

 estout A1 A2 using "$dir/Output/TablesFigures/results_rtrends.tex", style(tex) replace ///
    keep(cmH_*) ///
	cells(b(star fmt(%9.4f)) se(par)) ///
	nolabel collabels(none) mlabels(none) starlevels(* 0.10 ** 0.05 *** 0.01) ///
	varlabels(cmH_Pre6 "-6" cmH_Pre5 "-5" ///
	cmH_Pre4 "-4" cmH_Pre3 "-3" ///
	cmH_Pre2 "-2" cmH_Post0 "0" ///
	cmH_Post1 "1" cmH_Post2 "2" ///
	cmH_Post3 "3" cmH_Post4 "4" ///
	cmH_Post5 "5" cmH_Post6 "6")

local tex " \\ \hline"
local tex "`tex' Observations & `obs1' & `obs2' \\"
local tex "`tex' Mean Dep. Var & `meanA' & `meanB'   \\"
local tex "`tex' State X Birth Decade FE & Yes & Yes \\"
local tex "`tex' State X High CM F.E. & Yes & Yes  \\"
local tex "`tex' Census year F.E. & Yes & Yes  \\"
local tex "`tex' High CM X Birth Decade Trends & Yes & Yes  \\"
local tex "`tex' \multicolumn{3}{p{8cm}}{\tiny \textit{Notes:} Standard errors clustered at state-level."
local tex "`tex' *** p<0.01, ** p<0.05, * p<0.1.} \\ \end{tabular} }"
	
esttab A1 A2 using "$dir/Output/TablesFigures/results_rtrendsa.tex", style(tex) replace booktabs ///
	d(*) nolabel collabels(none) noobs postfoot("`tex'") nonum ///
	mtitles("(1)" "(2)") ///
	mgroups("Urbanization (log residence pop'n')" "Occupational Income (Log)", pattern(1 1) ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
	

/**************************************
Figure A.13
**************************************/

* Mig Life (Ban vs No Ban)
gen mig_life_banvnoban = . 
replace mig_life_banvnoban = 1 if mig_life_ban == 1
replace mig_life_banvnoban = 0 if mig_life_noban == 1
la var mig_life_banvnoban "Interstate Migration (State with ban vs no ban)"



local outcomes mig_life_banvnoban

foreach outcome in `outcomes' {
	eventstudyinteract `outcome'  ///
	cmH ///
    cmH_P* ///
	[aweight = count_dor] , absorb(i.i##i.t i.year i.cmH#c.t i.i##i.cmH) vce(cluster i) ///
	cohort(Ei) control_cohort(nevertreated)
	su `outcome' [aw =  count_dor]  if e(sample) == 1
    local meanA = string(round(r(mean), .01))

	
 	matrix b_iw = e(b_iw) // Save the results for table output
 	matrix V_iw = e(V_iw)
	capture matrix V_iw = diag(e(V_iw))
 	erepost b = b_iw V = V_iw // ssc install erepost
 	esttab
	local bound = round(abs(_b[cmH_Post6]) + _se[cmH_Post6]*1.96, .01)
	local rnge = `bound'/3
	local rnge_use = round(`rnge', .01)
	
	
	event_plot e(b_iw)#e(V_iw), default_look ///
	stub_lag(cmH_Post#) stub_lead(cmH_Pre#) ///
		graph_opt(ytitle("`: variable label `outcome'' (mean = `meanA')") xtitle("Birth decade relative to ban (+1)") xlabel(-6(1)6) ylabel(-`bound'(`rnge_use')`bound') legend(off) )
	graph copy ngSA`outcome', replace
		}

		
		global sum

foreach outcome of local outcomes{
    global sum $sum ngSA`letter'`outcome'
}
graph combine $sum ,  title("")  xcommon // subtitle("(Sun & Abraham 2021)") rows(1) ysize(1.3) xsize(3) 

graph export "Output/TablesFigures/Fig_MigBannoBan.png", replace

